package com.nulldev.util.manage.legacy;

import java.text.DecimalFormat;
import java.util.concurrent.atomic.LongAdder;

public class FPSCounter extends Timer {
	private volatile double fps, time;
	private volatile double avg, avgTime;
	private final LongAdder ticks = new LongAdder();

	public FPSCounter() {
		super();
		this.fps = 0.0;
		this.UPDATE_RATE.lazySet(1000L);
	}

	public FPSCounter startNow() {
		this.start();
		return this;
	}

	@Override
	void updateF() {
		if (this.ticks.sum() > 0) {
			if (this.avg <= 0) {
				this.avg = this.ticks.sum();
			} else {
				this.avg = (this.fps + this.ticks.sum()) / 2;
			}

			if (this.avgTime <= 0) {
				this.avgTime = this.time;
			} else {
				this.avgTime = (this.avgTime + this.time) / 2;
			}
			this.fps = this.ticks.sum();
			this.ticks.reset();
		}
	}

	public void callFPS() {
		this.ticks.increment();
	}

	public double getFPS() {
		return this.fps;
	}

	public double getAVGFPS() {
		return this.avg;
	}

	private long startTime, endTime;

	public void timeStart() {
		this.startTime = System.currentTimeMillis();
	}

	public void timeEnd() {
		this.endTime = System.currentTimeMillis();
		this.time = this.endTime - this.startTime;
	}

	public double avgRenderTime() {
		return this.avgTime;
	}

	private final DecimalFormat format = new DecimalFormat("##.#");

	public String avgRenderTimeS() {
		return this.format.format(this.avgTime);
	}
}
